﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class provider_CarInsurance_ProviderCarDiscount : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICDIS";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }

        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
           
            if (Request.QueryString["DID"] != null)
            {
                Int64 ProviderBodyId = Convert.ToInt64(Request.QueryString["DID"].ToString());
                ShowProviderCarDiscountItem(ProviderBodyId);
            }

            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;
        }

    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarDiscount _cardiscount = new CarDiscount();
                CarDiscountDB _cardiscountdb = new CarDiscountDB();

                _cardiscount.ProviderCarDiscountId = CheckProviderCarDiscount(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), ddlDiscount.Text, ddlDescription.Text, ddlDiscountType.Text);
                int _trantype = 1;
                if (_cardiscount.ProviderCarDiscountId > 0)
                {
                    _trantype = 2;
                }
                _cardiscount.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
                _cardiscount.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());

                _cardiscount.DiscountName = ddlDiscount.Text;
                _cardiscount.Description = ddlDescription.Text;
                _cardiscount.Type = ddlDiscountType.Text;
                _cardiscount.Rate = Convert.ToSingle(txtRate.Text.ToString());
                _cardiscount.Status = true;

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _cardiscount.FInfo1 = float.Parse(txtNewRate.Text);
                    _cardiscount.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _cardiscount.FInfo1 = float.Parse(txtRate.Text);
                    _cardiscount.DInfo1 = CE.Global.General.GetUTCDateTime();


                }

                _cardiscount.ProviderCarDiscountId = _cardiscountdb.Insert(_cardiscount);
                if (_cardiscount.ProviderCarDiscountId > 0)
                {
                    InsertProviderAuthTransaction(_cardiscount.ProviderCarDiscountId, "CICarDiscount", _trantype, 100, _authcode, _cardiscount.ProviderCompanyId, _cardiscount.RefCEUserId);
                    ProviderUpdateSave(_cardiscount.ProviderCarDiscountId);
                    lblMessage.Text = "Updated successfully.";
                    //Response.Redirect("ViewProviderCarDiscount.aspx");
                }
                else
                {
                    lblMessage.Text = "Updating failed.";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarDiscount";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    protected Int64 CheckProviderCarDiscount(Int64 ProviderCompanyId, string discountname, string description, string type)
    {
        CarDiscount _cardiscount = new CarDiscount();
        CarDiscountDB _cardiscountdb = new CarDiscountDB();
        Int64 _retValue = 0;
        _cardiscount = _cardiscountdb.GetCarDiscount(ProviderCompanyId, discountname, description, type);
        if (_cardiscount != null)
        {
            _retValue = _cardiscount.ProviderCarDiscountId;
        }
        return _retValue;
    }

    protected void ShowProviderCarDiscountItem(Int64 ProviderCarDiscountID)
    {
        CarDiscount _cardiscount = new CarDiscount();
        CarDiscountDB _cardiscountdb = new CarDiscountDB();

        _cardiscount = _cardiscountdb.GetCarDiscount(ProviderCarDiscountID);

        if (_cardiscount != null)
        {
            ddlDiscount.Text = _cardiscount.DiscountName;
            ddlDescription.Text = _cardiscount.Description;
            ddlDiscountType.Text = _cardiscount.Type;
            txtRate.Text = _cardiscount.Rate.ToString();

            if (_cardiscount.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _cardiscount.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _cardiscount.DInfo1.Day;
            usrEffectivityDate.Month = _cardiscount.DInfo1.Month;
            usrEffectivityDate.Year = _cardiscount.DInfo1.Year;
        }

        else
        {
            txtRate.Text = "";
            divNewRate.Visible = false;
            rdoEffectivity.SelectedValue = "No";
        }

        lblMessage.Text = "";
    }

    protected void ddlDiscount_SelectedIndexChanged(object sender, EventArgs e)
    {
        Int64 DiscountId = 0;
        DiscountId = CheckProviderCarDiscount(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), ddlDiscount.Text, ddlDescription.Text, ddlDiscountType.Text);

        ShowProviderCarDiscountItem(DiscountId);
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/CarInsurance/ViewProviderCarDiscount.aspx");
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
        }
    }
}
